package com.xzx.mr.combiner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 统计单词出现的次数
 *
 * Combiner先对每一个mapTask的输出做一个汇总
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/21 21:32
 */
public class WordCountCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable outValue = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,
            InterruptedException {
        int count = 0;
        for (IntWritable value : values) {
            count = count + value.get();
        }
        outValue.set(count);
        // 这样在map阶段相当于已经统计好了，单词的个数，再reduce阶段就不需要了
        context.write(key, outValue);
    }
}
